﻿#include "projectmenu.h"
#include <QMenuBar>
#include <QCollator>
#include <treedata.h>

const QMap<ProjectMenu, QString> gMapMenuName = QMap<ProjectMenu, QString >({ \
    {kSort, QStringLiteral("排序")}, \
    {kRemove, QStringLiteral("移除")}, \
    {kUpMove, QStringLiteral("上移")}, \
    {kDownMove, QStringLiteral("下移")}, \
    {kRename, QStringLiteral("重命名")}, \
});

ProjectMenuBase::ProjectMenuBase(QModelIndex index, QObject *parent):QObject(parent),m_index(index)
{  
    m_widget = (QWidget*)parent;
    m_menu = new QMenu(m_widget);
    m_menu->setWindowFlag(Qt::FramelessWindowHint);
    m_menu->setAttribute(Qt::WA_TranslucentBackground);
    m_menu->setStyleSheet("QMenu{ background:rgba(255,255,255,1); border:1px solid #666666; border-radius: 2px; }"
                          "QMenu::item{ padding:8px 25px; color:rgba(51,51,51,1); font-size:12px; }"
                          "QMenu::icon { padding: 8px 10px 5px 20px;}"
                          "QMenu::item:hover{ background-color:#6EBFFF; }"
                          "QMenu::item:selected{ background-color:#6EBFFF; }");
}

ProjectMenuBase::~ProjectMenuBase()
{

}

int getTypeByQString(QVariant str)
{
    // bool
    if(str.type() == QMetaType::Bool) {
        return 0;
    }
    // number
    else if(str.type() == QMetaType::Int || str.type() == QMetaType::UInt || \
                                                                                  str.type() == QMetaType::LongLong || str.type() == QMetaType::ULongLong) {
        return 1;
    }
    // 字符串，数字排前
    else if(str.type() == QMetaType::QString && str.toString().contains(QRegExp("[0-9]")))
    {
        return 2;
    }
    // 字符串，小写字母排前
    else if(str.toString().contains(QRegExp("[a-z]")))
    {
        return 3;
    }
    // 字符串，大写字母排前
    else if(str.toString().contains(QRegExp("[A-Z]")))
    {
        return 4;
    }
    // 字符串，中文排前
    else if(str.toString().contains(QRegExp("[\\x4e00-\\x9fa5]+")))
    {
        return 5;
    }
    // 其他
    else
    {
        return 6;
    }
}

bool qstringSortByRules(QVariant strA, QVariant strB)
{
    if(getTypeByQString(strA) == getTypeByQString(strB))
    {
        return strA < strB;
    }
    else
    {
        return getTypeByQString(strA) < getTypeByQString(strB);
    }
}


void ProjectMenuBase::doMenuAction(ProjectMenu menuType)
{
    LPCusTreeNode nodeData = (LPCusTreeNode)(m_index.model()->data(m_index).value<void*>());
    switch(menuType)
    {
        case kUpMove:
        {
            if(nodeData && nodeData->parent != nullptr)
            {
            }
        }
            break;
        case kDownMove:
        {
            if(nodeData && nodeData->parent != nullptr)
            {
            }
        }
            break;
        case kSort:
            break;
        case kRemove:
        {
            if(nodeData && nodeData->parent != nullptr)
            {
            }
        }
            break;
        default:
            break;
    }
}

void ProjectMenuBase::appendAction(ProjectMenu menuType, QString strIconPath, bool bSeparator)
{
    QString strActionName = "";
    LPCusTreeNode nodeData = (LPCusTreeNode)(m_index.model()->data(m_index).value<void*>());

	if(gMapMenuName.contains(menuType))
	{
		strActionName = gMapMenuName.find(menuType).value();
	}
    QAction * action = new QAction(strActionName,m_widget);
	if(!strIconPath.isEmpty())
		action->setIcon(QIcon(strIconPath));
        //action->setShortcut(QKeySequence("R"));
    m_menu->addAction(action);
    if(bSeparator)
        m_menu->addSeparator();
    connect(action, &QAction::triggered, this, [=]()
	{
        doMenuAction(menuType);
	});

}

void ProjectMenuBase::showMenu()
{
    m_menu->exec(QCursor::pos());
}


//////////////////////////////////////目录/////////////////////////////////////////////
ProjectMenuDir::ProjectMenuDir(QModelIndex index, QObject *parent):ProjectMenuBase(index, parent)
{

}

ProjectMenuDir::~ProjectMenuDir()
{

}


void ProjectMenuDir::initAction()
{
    appendAction(kUpMove, ":/res/png/file_open.png");
    appendAction(kSort, ":/res/png/file_open.png", true);
    appendAction(kRemove, ":/res/png/file_open.png");
}

void ProjectMenuDir::doMenuAction(ProjectMenu menuType)
{
    LPCusTreeNode nodeData = (LPCusTreeNode)(m_index.model()->data(m_index).value<void*>());
    switch(menuType)
    {
    case kRemove:
        return ProjectMenuBase::doMenuAction(kRemove);
    case kDownMove:
        return ProjectMenuBase::doMenuAction(kDownMove);
    case kUpMove:
        return ProjectMenuBase::doMenuAction(kUpMove);
    default:
        break;
    }
}


//////////////////////////////////////文件/////////////////////////////////////////////
ProjectMenuFile::ProjectMenuFile(QModelIndex index, QObject *parent):ProjectMenuBase(index, parent)
{

}

ProjectMenuFile::~ProjectMenuFile()
{

}


void ProjectMenuFile::initAction()
{
    appendAction(kUpMove, ":/res/png/file_close.png");
    appendAction(kDownMove, ":/res/png/file_close.png", true);
    appendAction(kSort, ":/res/png/file_close.png", true);
    appendAction(kRemove, ":/res/png/file_close.png");
}

void ProjectMenuFile::doMenuAction(ProjectMenu menuType)
{
    LPCusTreeNode nodeData = (LPCusTreeNode)(m_index.model()->data(m_index).value<void*>());
    switch(menuType)
    {
    case kRemove:
        return ProjectMenuBase::doMenuAction(kRemove);
    case kDownMove:
        return ProjectMenuBase::doMenuAction(kDownMove);
    case kUpMove:
        return ProjectMenuBase::doMenuAction(kUpMove);
    default:
        break;
    }
}

